local Button = require('src/Widgets/Button')
local Color = require('libs/colorlibrary/colorlibrary')
local Graphics = require('src/graphics')
local i18n = require('libs/i18n')
local Image = require('src/Graphics/Image')
local Label = require('src/Widgets/Label')
local Picture = require('src/Widgets/Picture')
local State = require('src/State')
local Widget = require('src/Widgets/Widget')

local exitConfirmState = State:derive('ExitConfirmState')

-- state --
function exitConfirmState:new()
--     -- TODO переделать на более общий шрифт?
    self.FontWidth = g_fontMedium:getWidth('.')
    self.FontHeight = g_fontMedium:getHeight('.')

    local w, h = love.window.getMode()

    -- menu
--    local coverImage = love.graphics.newImage('images/textures/old_rusty_metal_texture_01.png')
--    local coverw = coverImage:getWidth()
--    local coverh = coverImage:getHeight()
    local coverImage = Image('OldRustyMetal')
    local coverw = coverImage.w
    local coverh = coverImage.h
    local xc = (w - coverw) / 2
    local yc = (h - coverh) / 2
    self.cover = Picture(xc, yc, coverw, coverh, coverImage.texture, Color.white, 'center')

    -- label
    self.confirmText = Label(70, 50, coverw - 140, nil, i18n.translate('DoYouWanToExit'), g_fontTypeWriterMedium, Color.black, 'center')


    -- TODO remove bh?
--    local pi = love.graphics.newImage('images/textures/paint_stroke_04.png')
    local pi = Image('PaintStroke')
--    self.pi = Picture(0, 225-15, coverw, nil, pi, Color.white, 'center')
    -- button highlight image
--    local bh = love.graphics.newImage('images/textures/paint_stroke_04.png')
    local bh = Image('PaintStroke')

    -- cover height = 450 / 5 steps (4 menu items + 1 period) = 90
    local step = 90
    local disp = 20

--    local by = coverh - pi:getHeight() - 30
    local by = coverh - pi.h - 30

    -- button Start Game
    self.buttonYes = Button(0, by, coverw / 2, step, 'Yes')
    local bsn = Label(0, 10, coverw / 2, nil, i18n.translate('Yes'), g_fontMedium, Color.black, 'center', 'top')
    local bsh = Label(0, 10, coverw / 2, nil, i18n.translate('Yes'), g_fontMedium, Color.white, 'center', 'top')
    local bshb = Picture(0, 0, coverw / 2, nil, pi.texture, Color.white, 'center', 'center')
    self.buttonYes:addChild(bshb, 'highlight')
    self.buttonYes:addChild(bsn, 'normal')
    self.buttonYes:addChild(bsh, 'highlight')

    -- button No
    self.buttonNo = Button(coverw / 2, by, coverw, step, 'No')
    local bon = Label(0, 10, coverw / 2, nil, i18n.translate('No'), g_fontMedium, Color.black, 'center', 'top')
    local boh = Label(0, 10, coverw / 2, nil, i18n.translate('No'), g_fontMedium, Color.white, 'center', 'top')
    local bohb = Picture(0, 0, coverw / 2, nil, pi.texture, Color.white, 'center', 'center')
    self.buttonNo:addChild(bohb, 'highlight')
    self.buttonNo:addChild(bon, 'normal')
    self.buttonNo:addChild(boh, 'highlight')


    self.cover:addChild(self.confirmText)

    self.cover:addChild(self.buttonYes)
    self.cover:addChild(self.buttonNo)

    -- sounds
--    self.soundMove = love.audio.newSource('sounds/pick.ogg', 'static')
--    self.soundAccept = love.audio.newSource('sounds/accept.ogg', 'static')

    -- sounds setup for menu
--    menuMain:setSndMove(self.soundMove)
--    menuMain:setSndSuccess(self.soundAccept)
end

function exitConfirmState:load()
    -- events
    ENGINE.EVENTS:hook('onBtnClick', self.click)
end

function exitConfirmState:update(dt)
--    menuMain:update()
    self.cover:update(dt)
    -- TODO для устранения багов
    self.cover:update(dt)
end

function exitConfirmState:draw()
    self.cover:draw()
    Graphics:ShowFPS()
end

function exitConfirmState:exit()
    -- events
    ENGINE.EVENTS:unhook('onBtnClick', self.click)
end

-- ----------

function exitConfirmState.click(button)
    if button.text == 'Yes' then
--        ENGINE.SM:Pop('ExitConfirmState')
--        ENGINE.SM:Pop('MapState')
--        ENGINE.SM:Pop('LocationState')
        ENGINE.SM:PopAll()
        ENGINE.SM:InitState('MainMenuState', 'MainMenu')
        ENGINE.SM:Switch('MainMenuState')
    end
    if button.text == 'No' then
        ENGINE.SM:Pop('ExitConfirmState')
    end
end

return exitConfirmState
